package com.pmgames.store.data
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import com.pmgames.store.events.JavaFlexStoreEvent

	public class JavaServiceHandler extends EventDispatcher
	{
		public var serviceURL:String = "";
		public var response:XML;
		
		public function JavaServiceHandler(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function callServer():void
		{
			if(serviceURL == "")
			{
				throw new Error("serviceURL is a required parameter");
				return;
			}
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,	handleResponse);
			loader.load(new URLRequest(serviceURL));
		}
		
		private function handleResponse(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.currentTarget);
			response = XML(loader.data.toString());
			dispatchEvent(new JavaFlexStoreEvent(JavaFlexStoreEvent.DATA_LOADED));
		}
		
	}
}